var urlUploadToAliyun = server_host + '/comment/wx/uploadImage.do';
var urlRate = server_host + '/userweb/comment/uploadComment.do';
// var urlRate = 'http://192.168.1.221:8080/userweb/comment/uploadComment.do';

var orderNum = getParameterByName('orderNum');

var appModule = angular.module('Rate', []);
appModule.controller('RateCtrl', ['$scope', '$http', function($scope, $http) {

	// $scope.aliyun_host = aliyun_host;

	$scope.defects = [];

	$scope.imgListLocal = [];
	$scope.imgList = [];

	$scope.formFields = {
		orderNum: orderNum,
		value: 5,
		other: '',
		text: '',
		//选中的图片
		imgs: '',
		hidden: 0
	};

	//评分分数
	$scope.value = 5;
	$scope.scores = [1, 2, 3, 4, 5];

	//商户缺点
	$scope.defects = ['餐饮', '服务', '环境', '设备'];

	//打分
	$scope.setScore = function(_score) {
		$scope.formFields.value = _score;
	}

	//选择商户的缺点
	$scope.selectDefect = function() {
		$scope.formFields.other = '';
		var defects = $('input.defect:checked');
		if (defects.length != 0) {
			for (var i = 0; i < defects.length; i++) {
				$scope.formFields.other += defects.eq(i).attr('data');
				if (i < defects.length - 1) {
					$scope.formFields.other += ',';
				}
			}
		}
	}

	//图片选择
	$scope.imgChoose = function() {
		wx.chooseImage({
			success: function(res) {
				console.log('选择成功' + JSON.stringify(res));
				if (res.localIds.length > 3) {
					alert('最多只能上传三张图片！');
				} else
					$scope.imgListLocal = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
			}
		});
	}

	//图片上传
	$scope.imgUpload = function() {
		if ($scope.uploadIndex < $scope.imgListLocal.length) {
			console.log('第' + $scope.uploadIndex + 1 + '次上传：');
			wx.uploadImage({
				localId: $scope.imgListLocal[$scope.uploadIndex], // 需要上传的图片的本地ID，由chooseImage接口获得
				isShowProgressTips: 1, // 默认为1，显示进度提示
				success: function(res) {
					console.log('上传成功');
					console.log(res.serverId);
					$http.post(urlUploadToAliyun + '?media_id=' + res.serverId).success(function(data, status, headers, config) {
						if (data.shortUrl != '') {
							$scope.imgList.push(data.shortUrl);
							$scope.uploadIndex++;
							$scope.imgUpload();
						}
					});
				}
			});
		} else {
			$scope.formFields.imgs = $scope.imgList.toString();
			$http.post(urlRate + jsonToUrl($scope.formFields)).success(function(data, status, headers, config) {
				console.log(JSON.stringify(data));
				if(data.data.isSuccess){
					location.href = 'thx_praise.html';
				}
			});
		}
	}

	$scope.submit = function() {
		$scope.uploadIndex = 0;
		$scope.imgUpload();
	}

}]);

function wxJssdkInit() {
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: server_host + '/h5wx/getWxTicket.do?url=' + encodeURIComponent(location.href.split('#')[0]),
		success: function(data) {
			wx.config({
				debug: false,
				appId: wxAppId,
				timestamp: data.timestamp,
				nonceStr: data.nonceStr,
				signature: data.signature,
				jsApiList: wxJsApi.concat(['chooseImage', 'uploadImage'])
			});
		}
	});
}

if (typeof WeixinJSBridge == "undefined") {
	if (document.addEventListener) {
		document.addEventListener('WeixinJSBridgeReady', wxJssdkInit, false);
	} else if (document.attachEvent) {
		document.attachEvent('WeixinJSBridgeReady', wxJssdkInit);
		document.attachEvent('onWeixinJSBridgeReady', wxJssdkInit);
	}
} else {
	wxJssdkInit();
}